// components/commentDetail/index.js
import {
  home
} from '../../../../../../../api/index'
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //接收评论数据
    commentList: {
      type: Array,
      value: []
    },
    //评论条数
    TotalCount: {
      type: Number,
      value: 1
    }
  },

  /**
   * 组件的初始数据
   */
  data: {},
  /**
   * 组件的方法列表
   */
  methods: {
    // 给一级评论点赞
    Ilike(e) {
      // console.log(e.currentTarget.dataset)
      const _this = this
      let ID = e.currentTarget.dataset.id
      const IsLike = e.currentTarget.dataset.islike
      let index = e.currentTarget.dataset.index //一级评论的index
      const obj = {
        ID
      }
      home[IsLike ? 'cancellikecomment' : 'likecomment'](obj).then((res) => {
        if (res.StatusCode === 200) {
          const num = IsLike ? this.data.commentList[index].LikeCount -= 1 : this.data.commentList[index].LikeCount += 1
          let comment = this.data.commentList[index];
          comment.IsLike = !comment.IsLike
          comment.LikeCount = num
          this.setData({
            commentList: this.data.commentList
          })
        }
      }).catch(err => {
        if (err.statusCode === 401) {
          // 未登录
          _this.shudLogin();
        }
      })
    },
    // 展开、收起操作
    async toggleShowSecondary(e) {
      const {
        index,
        id
      } = e.currentTarget.dataset
      let correctComment = this.data.commentList[index]
      correctComment.showSecondary = !correctComment.showSecondary
      if (correctComment.showSecondary) {
        // 获取二级评论
        let res = await home.getcreationsecondcomment({
          "PageIndex": 1,
          "PageSize": 100,
          "ID": id
        })
        console.log(res);
        correctComment.secondaryCommentList = res.Result.Datas
      }
      this.setData({
        commentList: this.data.commentList
      })
      // console.log(this.data.commentList,'设置后的commentList')
    },
    // 给二级评论点赞
    secondaryLike(e) {
      // index:一级评论的下标，secondaryindex：一级评论下二级评论的下标  islike：二级评论是否点赞 id：二级评论的id
      let _this = this
      const {
        index,
        islike,
        secondaryindex,
        id
      } = e.currentTarget.dataset
      const obj = {
        ID:id
      }
      home[islike ? 'cancellikecomment' : 'likecomment'](obj).then((res) => {
        if (res.StatusCode === 200) {
          const num = islike ? this.data.commentList[index].secondaryCommentList[secondaryindex].LikeCount -= 1 : this.data.commentList[index].secondaryCommentList[secondaryindex].LikeCount += 1
          let comment = this.data.commentList[index].secondaryCommentList[secondaryindex];
          comment.IsLike = !comment.IsLike
          comment.LikeCount = num
          this.setData({
            commentList: this.data.commentList
          })
        }
      }).catch(err => {
        if (err.statusCode === 401) {
          // 未登录
          _this.shudLogin();
        }
      })
    },
    //去登录
    shudLogin() {
      const popup = this.selectComponent('#popupid');
      popup.setData({
        visible: true
      })
    },

    //预览风格大图片
    PreviewPicture(e) {
      wx.previewImage({
        current: e.currentTarget.dataset.atpresentimg, // 当前显示图片的http链接
        urls: e.currentTarget.dataset.imglist, // 需要预览的图片http链接列表
      });
    },
  },

  onReachBottom() {
    console.log('滚动位置:', e.detail.offset);
    this.getData()
  },
})